﻿package Naruto.Frames {	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.ui.Keyboard;
	import flash.utils.Timer;
	
	import flash.display.MovieClip; 
	import flash.display.Sprite;
	import flash.display.Stage;
	
	import Naruto.Bloods.*;
	import Naruto.*;

		
	public class Frame extends Sprite{

		protected var _appStart:Start;	
		
		protected var _status:Status;		
		
		protected var _stage:Stage;		
		protected var _loadPic:LoadPic;	
		
		protected var eventSprite:Sprite; 
		
		protected var me:Naruto;		
		protected var _blood:Blood;		
		protected var _health:Number;		
		protected var _ckl:int;			
		
		protected var _item:Item = null;	
		protected var _item1:Item1 = null;
		
		public var enemys:Array;
		
		public var flyshittest:ObjectHitTest;//飞镖碰撞检测
		public var flystestobj:Array;		//飞镖碰撞队列（对敌人）
		public var flyshurtmeobj:Array;  	//飞镖碰撞队列 (对主角)
		public var enemystestobj:Array; 	//敌人碰撞队列

		public var creataitimer:Timer;	
		public var creatAiAttackTimer:Timer;
		public var creatAshTimer:Timer; 
		
		public var enemynum:int;
		
		public var meattack:Attack;
		
		protected var isAshManStart:Boolean = false;	//防止因任务失败重新开始时Ashman由于不能remove导致敌人还未产生就判定没有敌人进入下一关
		protected var flag:int; 			
		protected var keycode:Array;		
		
		protected var _isDead:Boolean = false;
		protected var _isEnd:Boolean;		
		
		public function Frame(aStatus:Status, stage:Stage, appStart:Start){
			this._stage = stage;
//			trace(this._stage);
			this._stage.stageFocusRect = false;	//去掉焦点框。开始游戏时，切换到场景1，会产生焦点没在主角身上，导致键盘失效（需按下鼠标才能激活场景聚焦），因此使用了聚焦，此处是去掉聚焦的黄色框
			this._status = aStatus;
			this._appStart = appStart;
			this.isAshManStart = false;
			pubInit();
		}
		protected function pubInit():void{
			
			flyshittest = new ObjectHitTest();	//初始化碰撞检测
			enemystestobj = new Array();        //初始化敌人碰撞队列
			flystestobj = new Array();			//初始化敌人受飞镖攻击碰撞队列
			flyshurtmeobj = new Array();        //初始化主角受飞镖攻击碰撞队列
			
			me = new Naruto(this._status);
			
			this._blood = new Blood(this._stage);	
			this._health = this._status.getHealth();
			this._blood.changeHealth(this._health);	
			
			this._ckl = this._status.getCkl();		
			this._blood.changeCkl(this._ckl);	
			
			creatItem();		
			
			creatEnemys();					
			
			flyshittest.getflystestobj(enemystestobj,me,flystestobj);		//开始飞镖碰撞检测，参数(敌人队列，伤敌飞镖队列，伤我飞镖队列)
			this._stage.addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
//			creatAshman();		//只能放到子类里。因为会影响boss类的ashman
			
			flag = 0;
			keycode = [0];
			this._isEnd = false;
			
			this._stage.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			//this._stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			this._stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardEvent);
			this._stage.addEventListener(KeyboardEvent.KEY_UP,onKeyboardEvent);
			
			this.isAshManStart = true;
		}
		
		//加载道具框
		public function creatItem():void{
			var item:int = this._status.getCurrentItem();
			switch(item){
				case 0:{this._item = new Item();
						this._item.x = 800;
						this._item.y = 750;
						this._stage.addChild(_item);break;}
				case 1:{this._item1 = new Item1();
						this._item1.x = 800;
						this._item1.y = 750;
						this._stage.addChild(_item1);break;}
				default:return;
			}		
		}
		
		//改变道具
		public function changeItem(aNewItem:int) {
			if(this._status.getCurrentItem() != aNewItem){
				switch(this._status.getCurrentItem()){
					case 0:if(this._stage.contains(this._item)){this._stage.removeChild(this._item);};break;
					case 1:if(this._stage.contains(this._item1)){this._stage.removeChild(this._item1);}break;
					default:return;
				}
				switch(aNewItem){
					case 0:{this._item = new Item();
							this._item.x = 800;
							this._item.y = 750;
							this._stage.addChild(_item);
							break;}
					case 1:{this._item1 = new Item1();
							this._item1.x = 800;
							this._item1.y = 750;
							this._stage.addChild(_item1);
							break;}
				
				}
			}else{		//无变化
				return;
			}
		}
		
		public function creatEnemys():void{
		}

		public function CreatAI(event:Event):void{
		}
		
		//创建AI攻击
		public function creatAiAttack(){
			creatAiAttackTimer = new Timer(3000);
			creatAiAttackTimer.addEventListener(TimerEvent.TIMER,aiAttack);	//敌人开始攻击
			creatAiAttackTimer.start();
		}
		
		public function aiAttack(event:Event){
		}
		
		public function creatAshman(){
			creatAshTimer = new Timer(1000);
			creatAshTimer.addEventListener(TimerEvent.TIMER,Ashman);
			creatAshTimer.start();			
		}
		
		//清道夫
		public function Ashman(event:TimerEvent){
			
			//移除死亡者,将敌人在碰撞队列的位置置空（不能用splice删除，因为会导致数组错位问题），再从舞台移除
			var allClear:Boolean = true;	//是否还有敌人存在，影响本幕是否就结束
			for(var i=0;i<enemys.length;i++){
				if(enemys[i]!=null){
					allClear = false;
					//trace("enemys["+i+"].getStatus=" + enemys[i].getStatus());
					if(enemys[i].getStatus()==0){	//0:最高级别伤害（死亡）
						enemystestobj[i] = null;	//从碰撞队列移除
						enemys[i].removeGenin();
						enemys[i]=null;
					}
				}				
			}
			this._isEnd = allClear;
			if(this.isAshManStart == true){				
				if(this._isEnd == true){
					this.flag = 1;	//鼠标发镖事件停止。
					//移除所有监听器
					clearAllListener();
								
					var len:uint = this._stage.numChildren;
					while(len>0) {
						this._stage.removeChildAt(0);
						len--;
					}
					
					//保存角色状态
					//trace(this._blood.getHealth());
					this._status.setHealth(this._blood.getHealth());
					this._status.setCkl(this._blood.getCkl());
					
					changeFrame();
				}
			}
		}
		
		public function changeFrame(changeNum:int = 99){
		}
		
		public function switchChangeNum(changeNum:int, selfNum:int){
			if(changeNum == -4){
				//游戏失败时，移除所有
				//恢复血
				this._status.setHealth(100);
				this._blood.changeHealth(100);
				var len:uint = this._stage.numChildren;
				while(len>0) {
					this._stage.removeChildAt(len-1);
					len--;
				}
				this.isAshManStart = false;	//锁定Ashman!! 太关键了!!
				
				if(enemys){
					for(var i=0;i<enemys.length;i++){
						if(enemys[i]!=null){
							enemystestobj[i] = null;	//从碰撞队列移除
							enemys[i].removeGenin();
							enemys[i]=null;
						}				
					}
				}
				this.me = null;
				this._appStart._changeFrame(0);	//重新开始
			}else if(changeNum == -1){			//传入改变关数的值了(-1为Menu)
//				trace("menu");
				//回到菜单时，也移除所有
				//恢复血
				this._status.setHealth(100);
				this._blood.changeHealth(100);
				var len:uint = this._stage.numChildren;
				while(len>0) {
					this._stage.removeChildAt(len-1);
					len--;
				}
				this.isAshManStart = false;	//锁定Ashman!! 太关键了!!
				
				if(enemys){
					for(var i=0;i<enemys.length;i++){
						if(enemys[i]!=null){
							enemystestobj[i] = null;	//从碰撞队列移除
							enemys[i].removeGenin();
							enemys[i]=null;
						}				
					}
				}
				this.me = null;				
				this._appStart._changeFrame(-1);
			}else {
				this._appStart._changeFrame(selfNum);	//正常切换场景，已用Ashman移除过Sprite
			}
		}
		
		//移除所有监听器
		public function clearAllListener(){
			creatAshTimer.removeEventListener(TimerEvent.TIMER,Ashman);
			this._stage.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
			this._stage.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this._stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyboardEvent);
			this._stage.removeEventListener(KeyboardEvent.KEY_UP,onKeyboardEvent);
		}
		
		//任务失败
		public function missionFailed(levelNum:int,func:Function){
//			if(this._isMasked == false){
				this._loadPic.startMask();
//				this._isMasked = true;
//			}
			new MissonResult(this._stage,levelNum,false,func);		//参数：舞台，关卡数，结果(FALSE：失败；True：成功)，changeFrame
		}
		
		//鼠标监听
		public function onMouseDown(event:MouseEvent):void{
			if(this.flag==0){
				meattack = new Attack();
				flystestobj.push(meattack.MeStartAttack(this._stage,me.getme()));		//将//Flys1其加入飞镖碰撞检测队列
				flag = 1;
			}
		}
		
		//键盘监听
		public function onKeyboardEvent(event:KeyboardEvent):void{
			if (event.type=="keyDown"){
				if (keycode.indexOf(event.keyCode)!=-1) {// 已经按过了
					return;
				}
				else{
					keycode.push(event.keyCode);// 没按过，把键存起来
					addEventListener(Event.ENTER_FRAME,onEnterFrame);
				}
			}
			else{	//keyUp
				var tempIndex:int=-1;
				tempIndex=keycode.indexOf(event.keyCode);
				if(tempIndex>0){
					keycode.splice(tempIndex,1);// 按键弹起，从数组里清除
				}
			}
		}
		
		public function onEnterFrame(event:Event):void{	
		}
	}
}